#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;

vector<int> G[N];
int id[N], n, m;
char *s[N];
int len[N];

namespace tarjan {
    int low[N], dfn[N], clk, B, bl[N];
    vector<int> bcc[N];
    void init(int n) {
        B = clk = 0;
        memset(dfn, 0, sizeof(int) * (n+1));
        for (int i=0; i<n; ++i) bcc[i].clear();
    }

    void tarjan(int u) {
        static int st[N], p;
        static bool in[N];
        dfn[u] = low[u] = ++clk;
        st[p++] = u; in[u] = true;
        for (int &v : G[u]) {
            if (!dfn[v]) {
                tarjan(v);
                low[u] = min(low[u], low[v]);
            } else if (in[v]) low[u] = min(low[u], dfn[v]);
        }
        if (dfn[u] == low[u]) {
            while (1) {
                int x = st[--p]; in[x] = false;
                bl[x] = B; bcc[B].push_back(x);
                if (x == u) break;
            }
            ++ B;
        }
    }

    void go(int n) {
        init(n);
        for (int i=0; i<n; ++i) {
            if (!dfn[i]) tarjan(i);
        }
    }
}

namespace minrep {
    char a[N << 2];
    int n;
    int min_representation() {
        int i = 0, j = 1, k = 0;
        while (i < n && j < n && k < n) {
            int t = a[(i + k) % n] - a[(j + k) % n];
            if (t == 0) ++k;
            else {
                if (t > 0)
                    i += k + 1;
                else
                    j += k + 1;
                if (i == j)
                    ++j;
                k = 0;
            }
        }
        return min(i, j);
    }

    void doit(char s[]) {
        n = strlen(s);
        strcpy(a, s);
        strcpy(a+n, s);
        int st = min_representation();
        memcpy(s, a + st, n);
    }
}

int readstr(char *&s) {
    if (s != NULL) delete s;
    static char t[N];
    scanf("%s", t);
    int n = strlen(t);
    s = new char[n + 1];
    strcpy(s, t);
    return n;
}

int main(int argc, char const *argv[])
{
    while (~scanf("%d%d", &n, &m)) {
        for (int i=0; i<n; ++i) {
            len[i] = readstr(s[i]);
            minrep::doit(s[i]);
            G[i].clear();
        }

        for (int i=0; i<m; ++i) {
            int m, n;
            scanf("%d%d", &m, &n);
            --m; --n;
            G[m].push_back(n);
        }

        tarjan::go(n);
        
        int siz = tarjan::B;
        
        int ans = 0;
        for (int i=0; i<siz; ++i) {
            map<string, int> mp;
            for (int id : tarjan::bcc[i]) {
                // printf("i=%d, id=%d\n", i, id);
                mp[string(s[id])]++;
            }
            for (auto p : mp) ans = max(ans, p.second);
        }
        printf("%d\n", ans);
    }
    return 0;
}
